package com.kevin.util.properties;

import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.apache.commons.configuration2.BaseHierarchicalConfiguration;
import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.YAMLConfiguration;
import org.apache.commons.configuration2.builder.ReloadingFileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.FileBasedBuilderParameters;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.configuration2.reloading.PeriodicReloadingTrigger;
import org.yaml.snakeyaml.Yaml;

import com.kevin.util.PathUtil;

public class YamlTest {
    private ReloadingFileBasedConfigurationBuilder<YAMLConfiguration> reloadbuilder = null;
    private String testYaml = PathUtil.getProjectAbsolutePath() + "test.yaml";

    private YAMLConfiguration yamlConfiguration;

    public void setUp() throws Exception {

        Parameters params = new Parameters();
        // Read data from this file
        FileBasedBuilderParameters parameters = params.fileBased().setFileName("test.yaml");
        reloadbuilder = new ReloadingFileBasedConfigurationBuilder<YAMLConfiguration>(YAMLConfiguration.class).configure(parameters);
        reloadbuilder.setAutoSave(true);
        PeriodicReloadingTrigger trigger = new PeriodicReloadingTrigger(reloadbuilder.getReloadingController(), null, 1, TimeUnit.SECONDS);

        trigger.start();

        yamlConfiguration = reloadbuilder.getConfiguration();
//        yamlConfiguration = new YAMLConfiguration();
//        yamlConfiguration.read(new FileReader(testYaml));
    }

    public void testGetProperty_simple() {
        System.out.println("value1".equals(yamlConfiguration.getProperty("key1")));
    }

    public void testGetProperty_nested() {
        System.out.println("value23".equals(yamlConfiguration.getProperty("key2.key3")));
    }

    public void testGetProperty_nested_with_list() {
        System.out.println("key4.key5: " + yamlConfiguration.getProperty("key4.key5"));
    }

    public void testGetProperty_subset() {
        Configuration subset = yamlConfiguration.subset("key4");
        System.out.println("key5: " + subset.getProperty("key5"));
    }

    public void testGetProperty_very_nested_properties() {
        Object property = yamlConfiguration.getProperty("very.nested.properties");
        System.out.println("very.nested.properties: " + property);
    }

    public void testGetProperty_integer() {
        Object property = yamlConfiguration.getProperty("int1");
        System.out.println("property should be an Integer" + (property instanceof Integer));
        System.out.println(property);
    }

    public void testSave() throws IOException, ConfigurationException {
        // save the YAMLConfiguration as a String...
        StringWriter sw = new StringWriter();
        yamlConfiguration.write(sw);
        String output = sw.toString();

        // ..and then try parsing it back as using SnakeYAML
        Map parsed = new Yaml().loadAs(output, Map.class);
        System.out.println("testSave============");
        System.out.println(parsed.entrySet().size());
        System.out.println(parsed.get("key1"));

        Map key2 = (Map) parsed.get("key2");
        System.out.println(key2.get("key3"));

        List<String> key5 = (List<String>) ((Map) parsed.get("key4")).get("key5");
        System.out.println(key5.size());
        System.out.println(key5.get(0));
        System.out.println(key5.get(1));
        System.out.println("testSave============end");
    }

    public void testGetProperty_dictionary() {
        System.out.println(yamlConfiguration.getProperty("martin.name"));
        System.out.println(yamlConfiguration.getProperty("martin.job"));
        System.out.println(yamlConfiguration.getProperty("martin.skill"));
    }

    public void testCopyConstructor() {
        BaseHierarchicalConfiguration c = new BaseHierarchicalConfiguration();
        c.addProperty("foo", "bar");

        yamlConfiguration = new YAMLConfiguration(c);
        System.out.println("bar".equals(yamlConfiguration.getString("foo")));
    }

    public static void main(String[] args) {
        YamlTest test = new YamlTest();
        try {
            test.setUp();
            test.testGetProperty_simple();
            test.testGetProperty_nested();
            test.testGetProperty_nested_with_list();
            test.testGetProperty_subset();
            test.testGetProperty_very_nested_properties();
            test.testGetProperty_integer();
            test.testSave();
            test.testGetProperty_dictionary();
            test.testCopyConstructor();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        YamlUtil config = new YamlUtil("test.yaml");
//        config.save("keytest", "test1");
    }
}
